/*
 * Created on 2004-7-13
 *
 * Project: CashManagement
 */
package com.example.demo.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间查询工具类<p>
 * 主要实现时间点的上下限边界对齐
 * <p>
 * <p>
 * Project: CashManagement
 *
 * @author shen
 * <p>
 * 2004-7-13
 */
public class CalendarUtils {

    public final static int TODAY = 0;

    public final static int THIS_WEEK_FIRST_DAY = 1;
    public final static int THIS_WEEK_LAST_DAY = 2;

    public final static int LAST_WEEK_FIRST_DAY = 3;
    public final static int LAST_WEEK_LAST_DAY = 4;


    public final static int THIS_MONTH_FIRST_DAY = 5;
    public final static int THIS_MONTH_LAST_DAY = 6;


    public final static int LAST_MONTH_FIRST_DAY = 7;
    public final static int LAST_MONTH_LAST_DAY = 8;


    public final static int NEXT_DAY = 9;

    public final static int LAST_DAY = 10;

    public final static int THIS_QUARTER_FIRST_DAY = 11;
    public final static int THIS_QUARTER_LAST_DAY = 12;
    public final static int LAST_QUARTER_FIRST_DAY = 13;
    public final static int LAST_QUARTER_LAST_DAY = 14;

    /**
     * @param type
     * @return
     */
    public static Date getDate(int type) {
        return getDate(null, type);
    }

    final static int[] THIS_QUARTER_ADJUST = {0, -1, -2, 0, -1, -2, 0, -1, -2, 0, -1, -2,};
    final static int[] LAST_QUARTER_ADJUST = {-3, -4, -5, -3, -4, -5, -3, -4, -5, -3, -4, -5};

    private static Calendar getThisQuarterFirstDay(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, THIS_QUARTER_ADJUST[calendar.get(Calendar.MONTH)]);
        return calendar;
    }

    private static Calendar getLastQuarterFirstDay(Calendar calendar) {

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, LAST_QUARTER_ADJUST[calendar.get(Calendar.MONTH)]);
        return calendar;
    }

    public static Date getDate(Date date, int type) {

        Calendar calendar = Calendar.getInstance();
        if (date != null)
            calendar.setTime(date);


        switch (type) {
            case TODAY:
                break;

            case NEXT_DAY:
                calendar.add(Calendar.DATE, 1);
                break;

            case THIS_WEEK_FIRST_DAY:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                break;

            case THIS_WEEK_LAST_DAY:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
                break;

            case LAST_WEEK_FIRST_DAY:
                calendar.add(Calendar.WEEK_OF_YEAR, -1);
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                break;

            case LAST_WEEK_LAST_DAY:
                calendar.add(Calendar.WEEK_OF_YEAR, -1);
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
                break;

            case THIS_MONTH_FIRST_DAY:
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                break;

            case THIS_MONTH_LAST_DAY:
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                break;

            case LAST_MONTH_FIRST_DAY:
                calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                break;

            case LAST_MONTH_LAST_DAY:
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                break;

            case LAST_DAY:
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                break;

            case THIS_QUARTER_FIRST_DAY:
                calendar = getThisQuarterFirstDay(calendar);
                break;

            case THIS_QUARTER_LAST_DAY:
                calendar = getThisQuarterFirstDay(calendar);
                calendar.add(Calendar.MONTH, 3);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                break;

            case LAST_QUARTER_FIRST_DAY:
                calendar = getLastQuarterFirstDay(calendar);
                break;

            case LAST_QUARTER_LAST_DAY:
                calendar = getLastQuarterFirstDay(calendar);
                calendar.add(Calendar.MONTH, 3);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                break;

            default:

        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


}
